/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.nuage.tiow.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.nuage.tiow.vo.campaign.Campaign;
import org.nuage.tiow.vo.campaign.SystemState;
import org.nuage.tiow.vo.system.Planet;
import org.nuage.tiow.vo.system.PlanetType;
import org.nuage.tiow.vo.system.SolarSystem;
import org.nuage.tiow.vo.system.SystemLocation;
import org.nuage.tiow.vo.user.Player;

/**
 *
 * @author quentina
 */
public class CampaignCreator {

    public Campaign createCampaign(String name, String systemName, List<Player> players) {
        SystemLocation systemLocation = generateSytemLocation();
        List<Planet> planets = new ArrayList<Planet>();
        Map<Player, List<Planet>> playerControls = new HashMap<Player, List<Planet>>();
        for (Player player : players) {
            List<Planet> playerPlanets = new ArrayList<Planet>();
            int nbPlanets = DiceManager.rollD3();
            if ((systemLocation == SystemLocation.IMPERIAL && player.getWarhost().isImperial())
                    || (systemLocation == SystemLocation.HOSTILE && !player.getWarhost().isImperial())) {
                nbPlanets++;
            }
            for (int i = 1; i <= nbPlanets; i++) {
                Planet planet = generatePlanet("Planet " + i + " of " + player.getName());
                planets.add(planet);
                playerPlanets.add(planet);
            }
            playerControls.put(player, playerPlanets);
        }
        SystemState systemInitialState = new SystemState(playerControls);
        SolarSystem solarSystem = new SolarSystem(systemName, systemLocation, planets);
        return new Campaign(name, solarSystem, players, systemInitialState);
    }

    private SystemLocation generateSytemLocation() {
        return DiceManager.plusFour() ? SystemLocation.IMPERIAL : SystemLocation.HOSTILE;
    }

    private PlanetType generatePlanetType() {
        return PlanetType.values()[DiceManager.rollD6() - 1];
    }

    private Planet generatePlanet(String name) {
        PlanetType type = generatePlanetType();
        int critialObjectives = DiceManager.rollD3() + 2;
        return new Planet(name, critialObjectives, type);
    }
}
